package net.kts.story.twitter.contator;

import java.io.File;
import java.util.List;

import net.kts.story.twitter.oauth.OAuth;
import twitter4j.GeoLocation;
import twitter4j.Paging;
import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.ResponseList;
import twitter4j.Status;
import twitter4j.StatusUpdate;
import twitter4j.Tweet;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.auth.Authorization;
import twitter4j.internal.http.HttpResponseCode;
import android.app.Activity;
import android.util.Log;

public class TwitterConator{
	private static final String HASH_TAG = "#KoreaTravel ";
	private OAuth oAuth;
	private Twitter twitter;
	
	public TwitterConator(Activity activity, OAuth oAuth) throws TwitterException {
		super();
		this.oAuth = oAuth;
		this.twitter = oAuth.findTwitter();
	}

	public Status getHomeLine() throws TwitterException{
		Paging paging = new Paging();
		paging.setCount(1);
		paging.setPage(1);
		ResponseList<Status> homeTimeline = twitter.getHomeTimeline(paging);
		return homeTimeline.get(0);
	}
	
	public List<Tweet> listFindByGeoLocation(double tatitude, double longitude) throws TwitterException {
//		Log.d("twitter","list : "+tatitude+" "+longitude);
		Query query = new Query();
		query.setQuery("#KoreaTravel");
		query.setGeoCode(new GeoLocation(tatitude, longitude), 10, "mi");
		QueryResult result = twitter.search(query);
	    return result.getTweets();
	}
	
	public void submit(String message) throws TwitterException {
		if(!isAuthMemberTwitter())
			oAuth.authMemberInformation();
		try {
			Log.d("twitter","submit : "+message);
			twitter.updateStatus(HASH_TAG+message);
		} catch (TwitterException te) {
			errorHandler(te);
			throw te;
		}
	}
	
	public boolean submitByGeoLocation(String message, GeoLocation location) throws TwitterException {
		if(!isAuthMemberTwitter()){
			if(!oAuth.authMemberInformation())
				return false;
		}
		try {
			Log.d("twitter","submit : "+message);
			StatusUpdate update = new StatusUpdate(HASH_TAG+message);
			update.setLocation(location);
			update.location(location);
			twitter.updateStatus(update);
		} catch (TwitterException te) {
			errorHandler(te);
			throw te;
		}
		return true;
	}
	public boolean submitImageByGeoLocation(String message, File file,
			GeoLocation location) throws TwitterException {
		if(!isAuthMemberTwitter()){
			if(!oAuth.authMemberInformation())
				return false;
		}
		try {
			Log.d("twitter", "submit : " + message);
			StatusUpdate update = new StatusUpdate(HASH_TAG + message);
			update.setMedia(file);
			update.setLocation(location);
			twitter.updateStatus(update);
		} catch (TwitterException te) {
			errorHandler(te);
			throw te;
		}
		return true;
	}
	public ResponseList<Status> listTwitter(int pageNum, int count) throws TwitterException {
		//Paging page = new Paging(pageNum, count);
		return twitter.getUserTimeline();
	}

	private void errorHandler(TwitterException te) {
		if (HttpResponseCode.FORBIDDEN == te.getStatusCode()) {
			String strError = null;
			strError = te.getErrorMessage();
			if (true == strError.contains("duplicate")) {
				Log.e("twitter","duplicate"); 
			} else if (true == strError.contains("140")) {
				Log.e("twitter","140"); 
			} else {
				Log.e("twitter","error"); 
			}
		} else if (HttpResponseCode.UNAUTHORIZED == te.getStatusCode()) {
			Log.e("twitter","UNAUTHORIZED");
		}
	}

	public void obtainCertification(String pin) {
		oAuth.obtainCertification(pin);
	}
	
	public boolean isAuthMemberTwitter(){
		try {
			if(twitter.getOAuthAccessToken() == null)
				return false;
			Log.e("twitter",twitter.getOAuthAccessToken().getUserId()+"");
			if(twitter.getOAuthAccessToken().getUserId() != 0)
				return true;
		} catch (TwitterException e) {
			return false;
		}
		return false;
	}

	
	
}
